package gui;

import gamemngr.GameMngr;

import java.util.Arrays;

import logic.Country;
import logic.moves.ExchangeMove;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import com.swtdesigner.SWTResourceManager;

public class ChangeHouseRulesDialog extends Dialog{
	
	//private FormToolkit toolkit = new FormToolkit(Display.getCurrent());
	protected Shell shell;
	private GameMngr game;
	private RiskGUI gui;
	
	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public ChangeHouseRulesDialog(Shell parent, int style) {
		super(parent, style);
	}
	
	/**
	 * Create the dialog
	 * @param parent
	 */
	public ChangeHouseRulesDialog(Shell parent) {
		this(parent, SWT.NONE);
	}
	public ChangeHouseRulesDialog(Shell parent, GameMngr game) {
		this(parent, SWT.NONE);
		this.game = game;
	}
	public ChangeHouseRulesDialog(Shell parent, GameMngr game, RiskGUI gui) {
		this(parent, SWT.NONE);
		this.game = game;
		this.gui = gui;
	}
	
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	//	if(move != null)
	//		return move.getBriefDo();
	//	else
     		return "\n";
	}
	
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setLocation(RiskGUI.getNewPosition(getParent()));
		shell.setSize(513, 305);
		shell.setText("Change House Rules");

		final Label limitNumberOfLabel = new Label(shell, SWT.NONE);
		limitNumberOfLabel.setText("Limit the number of moves each turn?");
		limitNumberOfLabel.setBounds(20, 31, 192, 22);

		final Label limitNumberOfLabel_1 = new Label(shell, SWT.NONE);
		limitNumberOfLabel_1.setBounds(20, 60, 286, 22);
		limitNumberOfLabel_1.setText("What should be the maximum number of moves each turn?");

		final Label limitNumberOfLabel_2 = new Label(shell, SWT.NONE);
		limitNumberOfLabel_2.setBounds(20, 120, 192, 22);
		limitNumberOfLabel_2.setText("Limit the size of the reenforcement?");

		final Label limitNumberOfLabel_2_1 = new Label(shell, SWT.NONE);
		limitNumberOfLabel_2_1.setBounds(20, 146, 286, 22);
		limitNumberOfLabel_2_1.setText("What should be the maximum size of the reenforcement?");

		final Label label = new Label(shell, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		if(game.getNumMoveRule() == -1) {
			label.setText("");
			label.setEnabled(false);
		}
		else {
			label.setText(Integer.toString(game.getNumMoveRule()));
			label.setEnabled(true);
		}
		label.setBounds(465, 60, 32, 30);

		final Label label_1 = new Label(shell, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		if(game.getMaxReenforcement() == Integer.MAX_VALUE) {
			label_1.setText("");
			label_1.setEnabled(false);
		}
		else {
			label_1.setText(Integer.toString(game.getMaxReenforcement()));
			label_1.setEnabled(true);
		}
		label_1.setBounds(465, 146, 32, 26);
		
		final Scale scale_1 = new Scale(shell, SWT.NONE);
		if(game.getMaxReenforcement() == Integer.MAX_VALUE) {
			scale_1.setSelection(0);
			scale_1.setEnabled(false);
		}
		else {
			scale_1.setSelection(game.getMaxReenforcement());
			scale_1.setEnabled(true);
		}
		scale_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				label_1.setText(Integer.toString(scale_1.getSelection()));
			}
		});
		scale_1.setBounds(305, 140, 158, 42);
		scale_1.setMinimum(1);
		scale_1.setMaximum(100);
		
		final Button yesButton_1 = new Button(shell, SWT.CHECK);
		if(game.getMaxReenforcement() == Integer.MAX_VALUE)	yesButton_1.setSelection(false);
		else yesButton_1.setSelection(true);
		yesButton_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				if(yesButton_1.getSelection()) {
					scale_1.setEnabled(true);		
					label_1.setEnabled(true);
				} else {
					scale_1.setEnabled(false);		
					label_1.setEnabled(false);
				}
			}
		});
		yesButton_1.setBounds(305, 120, 83, 16);
		yesButton_1.setText("yes");

		final Scale scale = new Scale(shell, SWT.NONE);
		if(game.getNumMoveRule() == -1) {
			scale.setSelection(0);
			scale.setEnabled(false);
		}
		else {
			scale.setSelection(game.getNumMoveRule());
			scale.setEnabled(true);
		}
		scale.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				label.setText(Integer.toString(scale.getSelection()));
			}
		});
		scale.setBounds(305, 48, 158, 42);
		scale.setMinimum(1);
		scale.setMaximum(100);
		
		final Button yesButton_1_1 = new Button(shell, SWT.CHECK);
		if(game.getNumMoveRule() == -1) yesButton_1_1.setSelection(false);
		else yesButton_1_1.setSelection(true);
		yesButton_1_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				if(yesButton_1_1.getSelection()) {
					scale.setEnabled(true);
					label.setEnabled(true);
				} else {
					scale.setEnabled(false);
					label.setEnabled(false);
				}
			}
		});
		yesButton_1_1.setBounds(305, 30, 83, 16);
		yesButton_1_1.setText("yes");

		final Label limitNumberOfLabel_2_2 = new Label(shell, SWT.NONE);
		limitNumberOfLabel_2_2.setBounds(20, 198, 204, 22);
		limitNumberOfLabel_2_2.setText("Move soldiers more than once each turn?");

		final Button yesButton_1_2 = new Button(shell, SWT.CHECK);
		if(game.getLimitMove()) yesButton_1_2.setSelection(false);
		else yesButton_1_2.setSelection(true);
		yesButton_1_2.setBounds(305, 200, 83, 16);
		yesButton_1_2.setText("yes");

		final Button okButton = new Button(shell, SWT.NONE);
		okButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				if(scale.isEnabled()){ 
					game.setNumMoveRule(scale.getSelection());
					//TODO gui.addToLog("\r\n The number of moves allowed in each turn is: " + game.getNumMoveRule() + "\r\n");
				}
				if(scale_1.isEnabled()){
					game.setMaxReenforcement(scale_1.getSelection());
					//TODO gui.addToLog("\r\n The maximum reenforcement in each turn is: " + game.getMaxReenforcement()+ "\r\n");
				}
				if(yesButton_1_2.getSelection()){
					game.setLimitMove(false);
					// TODO gui.addToLog("\r\n There is no limit on the number of move phase at the end of each turn \r\n");
				} else {
					if(!game.getLimitMove()) {
						game.setLimitMove(true);
//						TODO gui.addToLog("\r\n Only one move phase allowed at the end of each turn \r\n");
					}
				}
					
				shell.dispose();
			}
		});
		okButton.setText("OK");
		okButton.setBounds(130, 240, 71, 22);

		final Button okButton_1 = new Button(shell, SWT.NONE);
		okButton_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				shell.dispose();
			}
		});
		okButton_1.setBounds(220, 240, 71, 22);
		okButton_1.setText("Cancel");

		final Button restoreDefultButton = new Button(shell, SWT.NONE);
		restoreDefultButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				game.setNumMoveRule(-1);
				game.setMaxReenforcement(Integer.MAX_VALUE);
				game.setLimitMove(true);
				label.setText("");
				label_1.setText("");
				scale.setSelection(0);
				scale_1.setSelection(0);
				scale.setEnabled(false);
				scale_1.setEnabled(false);
				yesButton_1.setSelection(false);
				yesButton_1_1.setSelection(false);
				yesButton_1_2.setSelection(false);
//				TODO gui.addToLog("\r\n Default rules have been restored \r\n");
			}
		});
		restoreDefultButton.setText("Restore Default");
		restoreDefultButton.setBounds(310, 240, 83, 23);
		
	}

    
	

}
